package com.service.admin.admin.controller;

import com.google.common.collect.Maps;
import com.service.admin.admin.controller.request.RoleMenuReq;
import com.service.admin.admin.controller.request.RoleReq;
import com.service.admin.admin.service.SysMenuService;
import com.service.admin.admin.service.SysRoleService;
import com.service.core.rest.BaseController;
import com.service.core.rest.response.BaseResponse;
import com.service.core.rest.response.MultiResponse;
import com.service.core.rest.response.OneResponse;
import com.service.dao.model.SysRole;
import com.service.utility.common.UUIDUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <dl>
 * <dt>back-end</dt>
 * <dd>Description:</dd>
 * <dd>Copyright: Copyright (C) 2015</dd>
 * <dd>Company: </dd>
 * <dd>CreateDate: 2019年08月02日</dd>
 * </dl>
 *
 * @author
 */
@Slf4j
@Api(tags = "角色权限")
@RestController
@RequestMapping("/sys-role")
public class SysRoleController extends BaseController {

    @Autowired
    private SysRoleService menuRoleService;

    @Autowired
    private SysMenuService menuService;

    @GetMapping("/list")
    public MultiResponse listRole(){
        String uuid = UUIDUtil.randomUUID();
        log.info("请求开始 Msg: 请求参数：{}，UUID:{}", "", uuid);
        List<SysRole> resultList = menuRoleService.listSysRole();
        log.info("请求结束： UUID:{}", uuid);
        return successQueryMultiResponse(resultList);
    }

    @GetMapping("/menu/{roleId}")
    public OneResponse getRoleMenuById(@PathVariable("roleId") Integer roleId) {
        String uuid = UUIDUtil.randomUUID();
        log.info("请求开始 Msg: 请求参数：{}，UUID:{}", "", uuid);
        Map<String, Object> resultData = Maps.newHashMap();
        resultData.put("menuTree", menuService.listAllMenuTree());
        resultData.put("menuIds", menuRoleService.getMenusIdByRid(roleId));
        log.info("请求结束： UUID:{}", uuid);
        return successQueryOneResponse(resultData);
    }

    @PostMapping("/save-role")
    public BaseResponse saveRole(@ApiParam @RequestBody @Validated RoleReq roleReq){
        String uuid = UUIDUtil.randomUUID();
        log.info("请求开始 Msg: 请求参数：{}，UUID:{}", "", uuid);
        menuRoleService.saveRole(roleReq);
        log.info("请求结束： UUID:{}", uuid);
        return successSaveBaseResponse();
    }

    @PostMapping("/delete/{roleId}")
    public BaseResponse deleteRole(@PathVariable("roleId") Integer roleId){
        String uuid = UUIDUtil.randomUUID();
        log.info("请求开始 Msg: 请求参数：{}，UUID:{}", "", uuid);
        menuRoleService.deleteRole(roleId);
        log.info("请求结束： UUID:{}", uuid);
        return successSaveBaseResponse();
    }

    @PostMapping("/save-role-menu")
    public BaseResponse saveRoleMenu(@ApiParam @RequestBody @Validated RoleMenuReq roleMenu){
        String uuid = UUIDUtil.randomUUID();
        log.info("请求开始 Msg: 请求参数：{}，UUID:{}", "", uuid);
        menuRoleService.saveRoleMenu(roleMenu);
        log.info("请求结束： UUID:{}", uuid);
        return successSaveBaseResponse();
    }


}
